package reproductores;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MOC {

	public static void encolar(boolean solamenteEncolar, boolean unSoloTema, String url, String primerTema) {
		
		boolean versionNueva = false;
		
		String[] temp = {"bash","-c","mocp -V"};
		
		InputStream is = null;
		try {
			is = Runtime.getRuntime().exec(temp).getInputStream();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		BufferedReader br = new BufferedReader (new InputStreamReader (is));
		
		try {
			if ((br.readLine()).contains("2.5.0-alpha4") || (br.readLine()).contains("2.5.0-alpha5"))
				versionNueva = true;
			else
				versionNueva = false;
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		String argumentos = "";
		
		if (versionNueva)
			argumentos = "-qpa";
		else 
			argumentos = "-ap";
			
		if (solamenteEncolar) {
				
			if (unSoloTema) {
				String[] commandLin = new String[]{"mocp", "-a" , url};
					
				try {
					System.out.println(commandLin[0] + " " + commandLin[1] + " " + commandLin[2]);
					@SuppressWarnings("unused")
					Process procesoLin;
					procesoLin = Runtime.getRuntime().exec(commandLin, null, null);
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
			else {
				String[] commandLin = {"bash", "-c", "mocp -a " + url};
				System.out.println(commandLin[0] + " " + commandLin[1] + " " + commandLin[2]);
					
				try {
					Runtime.getRuntime().exec(commandLin);
				} catch (IOException e) {
					e.printStackTrace();
				} 
			}
		}
		else {
				
			if (unSoloTema) {
				
				String[] commandLin = new String[]{"mocp", argumentos , url};
				try {
					System.out.println(commandLin[0] + " " + commandLin[1] + " " + commandLin[2]);
					@SuppressWarnings("unused")
					Process procesoLin;
					procesoLin = Runtime.getRuntime().exec(commandLin, null, null);
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
			else {

				
				String[] mocp = {"bash", "-c", "mocp " + argumentos + " " + primerTema};
				System.out.println(mocp[0] + " " + mocp[1] + " " + mocp[2]);
				try {
					Runtime.getRuntime().exec(mocp);
				} catch (IOException e) {
					e.printStackTrace();
				} 
					
				String restoTemas[] = url.split(primerTema);
				
				if (restoTemas.length > 1) {
					mocp = new String[]{"bash", "-c", "mocp -a " + restoTemas[1]};
					System.out.println(mocp[0] + " " + mocp[1] + " " + mocp[2]);
					try {
						Runtime.getRuntime().exec(mocp);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	public static void playPause() {
		String[] mocp = {"bash", "-c", "mocp -G "};
		try {
			Runtime.getRuntime().exec(mocp);
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	public static void stop() {
		String[] mocp = {"bash", "-c", "mocp -s "};
		try {
			Runtime.getRuntime().exec(mocp);
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	public static void limpiarLista() {
		String[] mocp = {"bash", "-c", "mocp -c "};
		try {
			Runtime.getRuntime().exec(mocp);
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	public static void activarShuffle() {
		String[] mocp = {"bash", "-c", "mocp -o shuffle "};
		try {
			Runtime.getRuntime().exec(mocp);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void desactivarShuffle() {
		String[] mocp = {"bash", "-c", "mocp -u shuffle "};
		try {
			Runtime.getRuntime().exec(mocp);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
